package down.sallai.ml.util;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.net.URL;
import java.util.Properties;

/**
 * @description: $
 * @author: sallai
 * @time: 2022年1月10日 0010 下午 09:51:37 秒
 */
@Slf4j
public class PropertiesUtils {
    private  InputStream input;
    private final static String FILE_NAME = "application.yml";

    private PropertiesUtils(){};

    public static Object getVal(String key) {
        InputStream inputStream = getInputStream();
        Properties properties = new Properties();
        try {
            properties.load(inputStream);
            return properties.get(key);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("加载配置文件流失败");
        }
        return null;
    }

    public static Object setVal(String key,String val) {
        InputStream inputStream = getInputStream();
        Properties properties = new Properties();
        try {
            properties.load(inputStream);
            properties.setProperty(key, val);
            properties.store(getWriter(),"修改");
        } catch (IOException e) {
            e.printStackTrace();
            log.error("加载配置文件流失败");
        }
        return null;
    }

    private static InputStream getInputStream(){
        return PropertiesUtils.class.getClassLoader().getResourceAsStream("application.yml");
    }

    private static FileOutputStream getWriter(){
        URL resource = PropertiesUtils.class.getClassLoader().getResource("application.yml");
        String path = resource.getPath();
        log.info(path);
        File file = new File(path);
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            return fileOutputStream;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }


}
